package com.yunqiao.api.controller;

import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.service.api.FeedbackService;
import com.yunqiao.service.api.entity.FeedbackEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 意见反馈
 *
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-04 20:33:59
 */
@RestController
@RequestMapping("api/feedback")
@Api(tags = "FeedbackApiController", description = "意见反馈管理")
public class FeedbackApiController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 列表
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "status", value = "1待处理 2已处理", required = true, defaultValue = "1", allowableValues = "1,2", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询意见反馈 列表")
    public ApiResponse<PageUtils<FeedbackEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = feedbackService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个意见反馈")
    public ApiResponse<FeedbackEntity> info(@PathVariable("id") Long id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        FeedbackEntity feedback = feedbackService.getById(id);
        return new ApiResponse<>(feedback);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存意见反馈")
    public ApiResponse save(@RequestBody FeedbackEntity feedback) {
        ValidatorUtils.validateEntity(feedback);
        feedbackService.save(feedback);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改意见反馈")
    public ApiResponse update(@PathVariable Long id, FeedbackEntity feedback) {
        FeedbackEntity dbItem = feedbackService.getById(id);
        if (dbItem == null) {
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        feedbackService.updateById(feedback);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除意见反馈")
    public ApiResponse delete(@PathVariable Long id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        feedbackService.removeById(id);
        return new ApiResponse<>();
    }

}
